<?php
namespace Admin\Controller;

use Common\Controller\AuthController;

class MessageController extends AuthController
{
    /**
     * 回复类型1：文本;2：图文;
     */
    const MSG_TEXT_TYPE = 1;
    const MSG_IMAGE_TEXT_TYPE = 2;


    //文本回复列表
    public function text_list()
    {

        $arr = D('Keywords')->msgList(self::MSG_TEXT_TYPE);
        $this->assign('_list', $arr['list']);
        $this->assign('_page', $arr['page']->show());
        $this->assign('title', '文本回复');
        $this->display();
    }

    //图文回复列表
    public function imageTextList()
    {
        $arr = D('Keywords')->msgList(self::MSG_IMAGE_TEXT_TYPE);
        $this->assign('_list', $arr['list']);
        $this->assign('_page', $arr['page']->show());
        $this->assign('title', '图文回复');
        $this->display();
    }

    //新增图文回复
    public function addImageText($kid = '')
    {

        if (IS_POST) {
            unset($_POST['file']);
            if (!D('Keywords')->create()) {
                $this->error(D('Keywords')->getError());
            } else {
                if (!empty($kid)) {
                    if (M('Keywords')->where("kid=%d", $kid)->save($_POST)) {
                        $this->success('更新图文消息成功');
                    } else {
                        $this->error('更新图文消息失败');
                    }
                } else {
                    $_POST['c_time'] = time();
                    $_POST['msg_type'] = self::MSG_IMAGE_TEXT_TYPE;
                    if (M('Keywords')->add($_POST)) {
                        $this->success('新增图文消息成功');
                    } else {
                        $this->error('新增图文消息失败');
                    }
                }

            }
        } else {

            if (!empty($kid)) {
                $re = M('Keywords')->where("kid=%d", $kid)->find();
                $this->assign('re', $re);
            }
            $this->assign('title', '编辑图文消息');
            $this->display();
        }

    }

    //新增文本回复
    public function addTextMsg($kid = '')
    {

        if (IS_POST) {
            $obj = D('Keywords');
            if (!$obj->create()) {
                $this->error($obj->getError());
            } else {
                $_POST['c_time'] = time();
                if ($_POST['kid'] == null) {
                    if ($obj->add($_POST)) {
                        $this->success('新增成功');
                    } else {
                        $this->error('新增失败');
                    }
                } else {
                    if ($obj->where("kid=%d", $_POST['kid'])->save($_POST)) {
                        $this->success('更新成功');
                    } else {
                        $this->error('更新失败');
                    }
                }

            }
        } else {
            if (!empty($kid)) {
                $re = M('Keywords')->where("kid=%d", $kid)->find();
                $this->assign('re', $re);
            }
            $this->assign('title', '编辑文本回复');
            $this->display();
        }

    }

    //删除文本回复
    public function delTextMsg($kid = '')
    {
        if (IS_AJAX) {
            if (M('keywords')->where("kid=%d", $kid)->delete()) {
                $this->success('删除成功', '', IS_AJAX);
            } else {
                $this->error('删除失败', '', IS_AJAX);
            }
        }

    }
    //图文详情页
    public function imageTextDetail($kid='')
    {

        $re = M('Keywords')->where("kid=%d", $kid)->find();
        $this->assign('re', $re);
        $this->display();

    }



}
